
import 'package:flutter/cupertino.dart';
import 'package:login_sdk/dao/login_dao.dart';
import 'package:sqflite/sqflite.dart';

class DBManage{
  // <数据库名字，数据库实例>
  static final Map<String,dynamic> _storageMap = {};
  // 数据库名字
  final String _dbName;
  // 实例
  late Database _db;

  Database get db {
    return _db;
  }

  // 多实例，一个数据库一个实例
  DBManage._({required String dbName}):_dbName = dbName{
    _storageMap[_dbName] = this;
  }
  static Future<DBManage> instance({required String dbName}) async{
    if(!dbName.endsWith(".db")){
      dbName='$dbName.db';
    }
    var storage = _storageMap[dbName];
    storage ??= await DBManage._(dbName: dbName).init();
    return storage;
  }

  Future<DBManage> init() async{
    _db = await openDatabase(_dbName);
    debugPrint('db');
    return this;
  }
  void destroy(){
    _db.close();
    _storageMap.remove(_dbName);
  }
  static String getAccountHash(){
    return LoginDao.getAccountHash()??'test';
  }
}